#include "Donthuc.h"



Donthuc::Donthuc()
{
	heso = 0;
	somu = 0;
}

Donthuc::Donthuc(const Donthuc &dt)
{
	this->heso = dt.heso;
	this->somu = dt.somu;
}

Donthuc::Donthuc(string chuoi)
{
	Donthuc a;
	if (chuoi.empty())
	{
		heso = 0;
		somu = 0;
		return;
	}
	int found;
	//tach phan he so a trong ax^t
	found = chuoi.find('x');
	if (found != string::npos)
	{
		heso = stoi(chuoi.substr(0, found));

		//tach phan so mu t trong ax^t
		found = chuoi.find('^');
		if (found != string::npos)
		{
			somu = stoi(chuoi.substr(found + 1, chuoi.size() - found - 1));
		}
	}
	else
	{
		heso = stoi(chuoi);
		somu = 0;
	}
}


Donthuc::~Donthuc()
{
}

Donthuc  Donthuc::operator+(const Donthuc &dt)
{
	Donthuc temp;
	if (this->somu == dt.somu)
	{
		temp.heso = this->heso + dt.heso;
		temp.somu = this->somu;
	}

	return temp;
}

Donthuc  Donthuc::operator*(const Donthuc &dt)
{
	Donthuc temp;
	temp.heso = this->heso * dt.heso;
	temp.somu = this->somu + dt.somu;

	return temp;
}

string Donthuc::ToString()
{
	string chuoi = "";
	if (this->heso == 0)
		return "";
	if (this->heso < 0)
		chuoi += " - " + std::to_string(abs(this->heso));
	else
		chuoi += std::to_string(this->heso);
	if (this->somu > 0)
		chuoi += "x^" + std::to_string(this->somu);

	return chuoi;
}


